clear all
cd "C:\Users\jose.balmori\Universidad Anahuac Dropbox\Jose Balmori\Paper 47 e-crime\Data\RawData\"
use "C:\Users\jose.balmori\Universidad Anahuac Dropbox\Jose Balmori\Paper 47 e-crime\Data\RawData\Mobility_Google.dta" 
drop if country_region_code!="MX"
drop sub_region_2 metro_area census_fips_code place_id

gen ndate = date(date, "YMD") 
format ndate %td 

keep if sub_region_1==""

rename retail_and_recreation_percent_ch retail_recreation
rename grocery_and_pharmacy_percent_cha grocery_pharma
rename parks_percent_change_from_baseli parks
rename transit_stations_percent_change_ transit_station
rename workplaces_percent_change_from_b workplace
rename residential_percent_change_from_ residential

label var retail_recreation "Retail and recreation"
label var grocery_pharma "Grocery and pharmacy"
label var parks "Parks"
label var transit_station "Transit stations"
label var workplace "Workplace"
label var residential "Residental"


 save "C:\Users\jose.balmori\Universidad Anahuac Dropbox\Jose Balmori\Paper 47 e-crime\Data\Final\mobility_national.dta", replace

g moyr=mofd(ndate)
format moyr %tm
 
collapse (mean) retail_recreation grocery_pharma parks transit_station workplace residential, by(moyr country_region)

g datevar = moyr
g lockdown = datevar>=722 & datevar<=724

format datevar %tm /*For daily data*/

set scheme sj

*drop if driving==.

tsset datevar
g max=80
g min=-80
 
twoway (area max datevar if lockdown==1, color(gs14))/*
*/ (area min datevar if lockdown==1, color(gs14)) /*
*/ (tsline retail_recreation, lpattern(solid) lwidth(medthick) color(gs1))/*
*/ (tsline residential, lpattern(shortdash) lwidth(medthick) color(gs4)) /*
*/ (tsline workplace, lpattern(longdash) lwidth(medthick) color(gs8)),/*
*/ ylabel(-80(20)80, angle(10)) tlabel(2020m2(2)2022m10, format("%tmMon/YY") angle(forty_five)) /*
*/ legend(order( 3 "Retail"  5 "Workplace" 4 "Residential" /*
*/ ) cols(3) pos(12) rowgap(0) size(*1)) /*
*/ xtitle("Date", size(large))  text(70 723 "Natl'" "Lock-" "down" , j(left) size(vsmall) )/*
*/ ytitle("Percentage Change Mobility (Google)" "Mexico's Mean Mobility (Baseline Jan/20)", size(med))

/*
twoway (area max datevar if lockdown==1, color(gs14))/*
*/ (area min datevar if lockdown==1, color(gs14)) /*
*/ (tsline retail_recreation, lpattern(solid) lwidth(thin) color(gs1))/*
*/ (tsline grocery_pharma, lpattern(shortdash) lwidth(thin) color(gs3))/*
*/ (tsline workplace, lpattern(longdash) lwidth(medthick) color(gs5))/*
*/ (tsline parks, lpattern(solid) lwidth(thin) color(gs7)) /*
*/ (tsline transit_station, lpattern(shortdash) lwidth(thin) color(gs9)) /*
*/ (tsline residential, lpattern(longdash) lwidth(medthick) color(gs11)), /*
*/ ylabel(-80(20)80, angle(10)) tlabel(2020m3(2)2022m9, format("%tmMon/YY") angle(forty_five)) /*
*/ legend(order( 3 "Retail" 4 "Grocery-Pharma" 5 "Workplace" /*
*/  6 "Parks" 7 "Transit St." 8 "Residential") cols(3) pos(12) rowgap(0) size(*1)) /*
*/ xtitle("Date", size(large))  text(70 723.25 "National" "Lockdown" , j(left) size(vsmall) )/*
*/ ytitle("Percentage Change Mobility (Google)" "Mexico's Mean Mobility (Baseline Jan/20)", size(med))
*/
local pathdata "C:\Users\jose.balmori\Universidad Anahuac Dropbox\Jose Balmori\Paper 47 e-crime\Paper Oct 23\output"
cd "`pathdata'"
 graph export "mobility.png", as(png) width(1000) replace

clear all
